package com.hk.jmm.app;

import android.app.Activity;
import android.content.Intent;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.hk.jmm.R;
import com.hk.jmm.app.fragment.AiFragment;
import com.hk.jmm.app.fragment.DmmFragment;
import com.hk.jmm.app.fragment.GcFragment;
import com.hk.jmm.app.fragment.MeFragment;
import com.hk.jmm.app.fragment.NewsFragment;
import com.hk.jmm.model.MainModel;
import com.hk.jmm.presenter.MainPresenter;
import com.skyz.base.activity.BaseMvpActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseMvpActivity<MainModel, MainActivity, MainPresenter> {
    public static void showActivity(Activity activity) {
        Intent intent = new Intent(activity, MainActivity.class);
        activity.startActivity(intent);
    }
    @Override
    protected void onMessageEvent(Object event) {

    }
    @Override
    protected int getLayoutId() {
        return R.layout.main_activity;
    }

    int[] tabIds = new int[]{R.id.home_ai,R.id.home_gc,R.id.home_dmm,R.id.home_news,R.id.home_me};
    List<TextView> tabs = new ArrayList<>();


    private int lastTabId = tabIds[0];
    @Override
    protected void onLayoutInflated() {
        setLayoutFullScreen(true);
        aiFragment = new AiFragment();
        gcFragment = new GcFragment();
        dmmFragment = new DmmFragment();
        newsFragment = new NewsFragment();
        meFragment = new MeFragment();
        fragmentManager=getSupportFragmentManager();
        FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fragment_container,aiFragment);
//        fragmentTransaction.add(R.id.fragment_container,gcFragment);
//        fragmentTransaction.add(R.id.fragment_container,dmmFragment);
//        fragmentTransaction.add(R.id.fragment_container,newsFragment);
        fragmentTransaction.add(R.id.fragment_container,meFragment);
//        fragmentTransaction.hide(gcFragment);
//        fragmentTransaction.hide(dmmFragment);
//        fragmentTransaction.hide(newsFragment);
        fragmentTransaction.hide(meFragment);

        fragmentTransaction.commit();
        for (int id:tabIds) {
            tabs.add((TextView)findViewById(id));
        }
        tabs.get(0).setSelected(true);
    }
    private FragmentManager fragmentManager;
    AiFragment aiFragment ;
    GcFragment gcFragment;
    DmmFragment dmmFragment;
    NewsFragment newsFragment;

    MeFragment meFragment;
    @Override
    protected void initData(Bundle savedInstanceState) {
        for (TextView tv:tabs) {
            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction();
                    int itemId=view.getId();
                    if(itemId==lastTabId){
                        return;
                    }
                    findViewById(lastTabId).setSelected(false);
                    view.setSelected(true);
                    switch (lastTabId){
                        case R.id.home_ai:fragmentTransaction.hide(aiFragment);break;
                        case R.id.home_gc:fragmentTransaction.hide(gcFragment);break;
                        case R.id.home_dmm:fragmentTransaction.hide(dmmFragment);break;
                        case R.id.home_news:fragmentTransaction.hide(newsFragment);break;
                        case R.id.home_me:fragmentTransaction.hide(meFragment);break;
                    }
                    switch (itemId){
                        case R.id.home_ai:fragmentTransaction.show(aiFragment);break;
                        case R.id.home_gc:fragmentTransaction.show(gcFragment);break;
                        case R.id.home_dmm:fragmentTransaction.show(dmmFragment);break;
                        case R.id.home_news:fragmentTransaction.show(newsFragment);break;
                        case R.id.home_me:fragmentTransaction.show(meFragment);break;
                    }
                    lastTabId=itemId;
                    fragmentTransaction.commit();
                }
            });
        }

    }

    @Override
    protected MainPresenter initMvpPresenter() {
        return new MainPresenter(this, getLifecycle());
    }
}
